/*
 *  Copyright 2019-2023 oyun
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.oyun.modules.document.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oyun.annotation.Log;
import com.oyun.modules.document.domain.BookClassification;
import com.oyun.modules.document.domain.vo.BookClassificationQueryCriteria;
import com.oyun.modules.document.service.BookClassificationService;
import com.oyun.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author oyun
 * @date 2024-05-10
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "书籍分类管理管理")
@RequestMapping("/api/bookClassification")
public class BookClassificationController {

    private final BookClassificationService bookClassificationService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('bookClassification:list')")
    public void exportBookClassification(HttpServletResponse response, BookClassificationQueryCriteria criteria) throws IOException {
        bookClassificationService.download(bookClassificationService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询书籍分类管理")
    @ApiOperation("查询书籍分类管理")
    @PreAuthorize("@el.check('bookClassification:list')")
    public ResponseEntity<PageResult<BookClassification>> queryBookClassification(BookClassificationQueryCriteria criteria, Page<Object> page) {
        return new ResponseEntity<>(bookClassificationService.queryAll(criteria, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增书籍分类管理")
    @ApiOperation("新增书籍分类管理")
    @PreAuthorize("@el.check('bookClassification:add')")
    public ResponseEntity<Object> createBookClassification(@Validated @RequestBody BookClassification resources) {
        bookClassificationService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改书籍分类管理")
    @ApiOperation("修改书籍分类管理")
    @PreAuthorize("@el.check('bookClassification:edit')")
    public ResponseEntity<Object> updateBookClassification(@Validated @RequestBody BookClassification resources) {
        bookClassificationService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除书籍分类管理")
    @ApiOperation("删除书籍分类管理")
    @PreAuthorize("@el.check('bookClassification:del')")
    public ResponseEntity<Object> deleteBookClassification(@RequestBody List<Long> ids) {
        bookClassificationService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/getParentType")
    @ApiOperation("获取父类型")
    public ResponseEntity<Object> getParentType() {
        return new ResponseEntity<>(bookClassificationService.getParentType(), HttpStatus.OK);
    }


    @ApiOperation("根据名称查询")
    @GetMapping("/getByName")
    public ResponseEntity<Object> getByUsername(@RequestParam String name) {
        return new ResponseEntity<>(bookClassificationService.getByName(name), HttpStatus.OK);
    }
}
